package com.example.mweather.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mweather.R;
import com.example.mweather.beans.PlaceBean;

import java.util.List;

public abstract class CityManageAdapter extends RecyclerView.Adapter<CityManageAdapter.RVHolder> {

    private List<PlaceBean> datas;

    public CityManageAdapter(List<PlaceBean> datas) {
        this.datas = datas;
    }

    public abstract void selectCity(PlaceBean city);

    public abstract void deleteCity(PlaceBean city);

    @NonNull
    @Override
    public CityManageAdapter.RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_city_manage, parent, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityManageAdapter.RVHolder holder, int position) {
        holder.tvCity.setText(datas.get(position).getName());
        holder.tvTemp.setText(datas.get(position).getTemp());
        holder.itemView.setOnClickListener(v -> selectCity(datas.get(position)));
        holder.itemView.setOnLongClickListener(v -> {
            deleteCity(datas.get(position));
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RVHolder extends RecyclerView.ViewHolder {
        private TextView tvCity;
        private TextView tvTemp;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvCity = itemView.findViewById(R.id.tvCity);
            tvTemp = itemView.findViewById(R.id.tvTemp);
        }
    }
}
